package com.mxpio.erp.mes.policy.impl;

import com.mxpio.erp.common.mes.entity.ManufactureWorkBatch;
import com.mxpio.erp.common.mes.entity.ManufactureWorkOrder;
import com.mxpioframework.common.util.BeanReflectionUtils;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import org.springframework.stereotype.Component;


@Component
public class ManufactureWorkOrderSavePolicy extends SmartCrudPolicyAdapter {
	
	@Override
	public boolean beforeInsert(CrudContext context) {
		Object o = context.getEntity();
		if(o instanceof ManufactureWorkOrder){
			return generateBatchInfo((ManufactureWorkOrder) o);
		}
		return true;
	}
	private boolean generateBatchInfo(ManufactureWorkOrder wo){
		ManufactureWorkBatch manufactureWorkBatch = new ManufactureWorkBatch();
		BeanReflectionUtils.copyProperties(manufactureWorkBatch,wo );
		JpaUtil.save(manufactureWorkBatch);
		return true;
	}
}
